﻿using System;
using System.Windows.Forms;
using Calendar.Domain;

namespace Calendar.GUI
{
    //public delegate void UpdateHandler();
    public partial class PomodoroForm : Form
    {
        TimerHandler _timerHandler;
        WorkRecordHandler _workRecordHandler;
        //public UpdateHandler UpdateEvent = null;
        

        public PomodoroForm(WorkRecordHandler workRecordHandler)
        {
            InitializeComponent();
            _timerHandler = new TimerHandler();
            _timerHandler.Update += UpdateComponent;
            _workRecordHandler = workRecordHandler;
            startButton.Enabled = true;
            stopButton.Enabled = false;

            int maxSteps = (int)thirtyTimerStepNumericUpDown1.Value;
            _timerHandler.SetStepsCount(maxSteps);
            //thirtyTimerStepNumericUpDown1.Value = 4;
            thirtyTimerStepsComboBox.Items.AddRange(new object[] { "第1步", "第2步", "第3步", "第4步" });
            thirtyTimerStepsComboBox.SelectedIndex = 0;
            thirtyTimerStepNumericUpDown2.Value = 1;

        }

        private void UpdateComponent()
        {
            timerLabel.BeginInvoke(new MethodInvoker(delegate { timerLabel.Text = _timerHandler.TimerLabelText; }));
            pomodoroProgressBar.BeginInvoke(new MethodInvoker(delegate { pomodoroProgressBar.Value = _timerHandler.PomodoroProgressBarValue; }));
            //thirtyTimerProgressLabel.BeginInvoke(new MethodInvoker(delegate { thirtyTimerProgressLabel.Text = _timerHandler.ThirtyTimerProgressText; }));
        }

        public void SetTimeAllocation(int selectedIndex)
        {
            timeSlotNumericUpDown.Value = _workRecordHandler.GetTimeSlot(selectedIndex);
            breakTimeNumericUpDown.Value = _workRecordHandler.GetBreakTime(selectedIndex);
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            _timerHandler.SetTimeAllocation((int)timeSlotNumericUpDown.Value, (int)breakTimeNumericUpDown.Value);
            _timerHandler.StartPomodoro();
            _workRecordHandler.CreateRecord((int)timeSlotNumericUpDown.Value, (int)breakTimeNumericUpDown.Value);
            startButton.Enabled = false;
            stopButton.Enabled = true;
            UpdateComponent();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            _timerHandler.StopPomodoro();
            _workRecordHandler.AddRecord();
            startButton.Enabled = true;
            stopButton.Enabled = false;
            UpdateComponent();
        }

       
        private void thirtyTimerButton_Click(object sender, EventArgs e)
        {
            this.Width = 600;
        }

        private void thirtyTimerStepNumericUpDown1_ValueChanged(object sender, EventArgs e)//選擇步數
        {
            int maxSteps = (int)thirtyTimerStepNumericUpDown1.Value;

            _timerHandler.SetStepsCount(maxSteps);

            thirtyTimerStepsComboBox.Items.Clear();
            thirtyTimerListBox.Items.Clear();
            thirtyTimerStepsComboBox.Items.AddRange(_timerHandler.GetStepCount());
            thirtyTimerStepsComboBox.SelectedIndex = 0;
            
        }

        private void thirtyTimerStepsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //thirtyTimerStepNumericUpDown2.Show();
            //Console.WriteLine("SelectedIndex: "+thirtyTimerStepsComboBox.SelectedIndex);
            //Console.WriteLine("SelectedItem: "+thirtyTimerStepsComboBox.SelectedItem);
            //Console.WriteLine("SelectedValue: "+thirtyTimerStepsComboBox.SelectedValue);
            int index = thirtyTimerStepsComboBox.SelectedIndex;
            thirtyTimerStepNumericUpDown2.Value = _timerHandler.GetTime(index);
            thirtyTimerTextBox.Text = _timerHandler.GetName(index);
            
            //thirtyTimerTextBox.Text = _timerHandler.GetName(thirtyTimerStepsComboBox.SelectedIndex);
           
            SetWorkStep();
        }
       
        private void thirtyTimerTextBox_TextChanged(object sender, EventArgs e)
        {
            SetWorkStep();
        }

        private void thirtyTimerStepNumericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            SetWorkStep();
        }
        public void SetWorkStep()
        {
            int id = thirtyTimerStepsComboBox.SelectedIndex;//從0開始
            string name = thirtyTimerTextBox.Text;
            int time = (int)thirtyTimerStepNumericUpDown2.Value;
            _timerHandler.SetWorkStep(id, name, time);

        }
        private void thirtyTimerTotallyWorkStepButton_Click(object sender, EventArgs e)
        {
            thirtyTimerListBox.Items.Clear();
            thirtyTimerListBox.Items.AddRange(_timerHandler.ShowWorkStepList());
        }

        private void thirtyTimerStartButton_Click(object sender, EventArgs e)
        {
            _timerHandler.StartThirtyTimer();
        }

        //private void thirtyTimerStepsComboBox_SelectedValueChanged(object sender, EventArgs e)
        //{
            
        //    SetWorkStep();
        //}

    }
}

